package com.tjec.project.portal.service.impl;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.portal.domain.BmsNotes;
import com.tjec.project.portal.domain.BmsNotesStakeholder;
import com.tjec.project.portal.domain.vo.BmsNotesVo;
import com.tjec.project.portal.mapper.BmsNotesMapper;
import com.tjec.project.portal.service.IBmsNotesService;
import com.tjec.project.portal.service.IBmsNotesStakeholderService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.vo.BmsAttachmentVo;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.service.IBmsAttachmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 便签Service业务层处理
 *
 * @author liuxu
 * @date 2020-05-12
 */
@Service
public class BmsNotesServiceImpl implements IBmsNotesService {
    @Autowired
    private BmsNotesMapper bmsNotesMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBmsNotesStakeholderService iBmsNotesStakeholderService;
    @Autowired
    private IBmsAttachmentService iBmsAttachmentService;
    @Autowired
    private IBmsProjectService iBmsProjectService;
    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;

    /**
     * 查询便签
     *
     * @param guid 便签ID
     * @return 便签
     */
    @Override
    public BmsNotes selectBmsNotesByGuid(String guid) {
        BmsNotes bmsNotes = bmsNotesMapper.selectBmsNotesByGuid(guid);
        //获取干系人
        BmsNotesStakeholder bmsNotesStakeholder = new BmsNotesStakeholder();
        bmsNotesStakeholder.setNotesId(bmsNotes.getGuid());
        bmsNotes.setStakeholderList(iBmsNotesStakeholderService.selectBmsNotesStakeholderList(bmsNotesStakeholder));
        //附件
        List<BmsAttachmentVo> bmsAttachmentVoList = iBmsAttachmentService.selectBmsAttachmentDownloadByBizIdNew(bmsNotes.getGuid(), AttachmentConstants.NOTES);
        List<String> list = bmsAttachmentVoList.stream().map(k -> k.getGuid()).collect(Collectors.toList());
        bmsNotes.setAttachmentGuids(list.toArray(new String[list.size()]));
        bmsNotes.setAttachmentList(bmsAttachmentVoList);
        return bmsNotes;
    }

    /**
     * 查询便签列表
     *
     * @param bmsNotes 便签
     * @return 便签
     */
    @Override
    public List<BmsNotes> selectBmsNotesList(BmsNotes bmsNotes) {
        List<BmsNotes> list = bmsNotesMapper.selectBmsNotesList(bmsNotes);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(item -> {
                BmsNotesStakeholder bmsNotesStakeholder = new BmsNotesStakeholder();
                bmsNotesStakeholder.setNotesId(item.getGuid());
                item.setStakeholderList(iBmsNotesStakeholderService.selectBmsNotesStakeholderList(bmsNotesStakeholder));
            });
        }
        return list;
    }

    @Override
    public List<BmsNotesVo> getProjectList(String guid,String userGuid) {
        return bmsNotesMapper.getProjectList(guid,userGuid);
    }

    @Override
    public List<BmsNotesVo> getBusinessList(String projectGuid, String guid) {
        return bmsNotesMapper.getBusinessList(projectGuid, guid);
    }

    @Override
    public List<BmsNotesVo> getStakeholderList(String projectGuid, String guid) {
        return bmsNotesMapper.getStakeholderList(projectGuid, guid);
    }

    /**
     * 新增便签
     *
     * @param bmsNotes 便签
     * @return 结果
     */
    @Override
    public int insertBmsNotes(BmsNotes bmsNotes) {
        //获取项目名称
        if(StringUtils.isNotBlank(bmsNotes.getProjectId())){
            getProjectName(bmsNotes);
        }
        bmsNotes.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        bmsNotes.setCreateTime(DateUtils.getNowDate());
        int ret = bmsNotesMapper.insertBmsNotes(bmsNotes);
        if (ret > 0) {
            if (StringUtils.isNotBlank(bmsNotes.getStakeholderId())) {
                insertStakeholder(bmsNotes);
            }
            String[] ss = bmsNotes.getAttachmentGuids();
            if (null != ss && ss.length > 0) {
                updateAttachment(bmsNotes);
            }
        }
        return ret;
    }

    /**
     * 获取项目名称
     * @param bmsNotes
     */
    private void getProjectName(BmsNotes bmsNotes) {
        if(StringUtils.isNotBlank(bmsNotes.getProjectId())){
            BmsProject bmsProject = iBmsProjectService.selectBmsProjectByGuid(bmsNotes.getProjectId());
            bmsNotes.setProjectName(bmsProject.getName());
        }
    }

    /**
     * 修改便签
     *
     * @param bmsNotes 便签
     * @return 结果
     */
    @Override
    public int updateBmsNotes(BmsNotes bmsNotes) {
        //获取项目名称
        getProjectName(bmsNotes);
        int ret = bmsNotesMapper.updateBmsNotes(bmsNotes);
        if (ret > 0) {
            if (StringUtils.isNotBlank(bmsNotes.getStakeholderId())) {
                insertStakeholder(bmsNotes);
            }
            String[] ss = bmsNotes.getAttachmentGuids();
            if (null != ss && ss.length > 0) {
                bmsAttachmentMapper.updateBmsAttachmentByBizId(new String[]{bmsNotes.getGuid()},tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
                updateAttachment(bmsNotes);
            }else {
            	bmsAttachmentMapper.updateBmsAttachmentByBizId(new String[]{bmsNotes.getGuid()},tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
            }
        }
        return ret;
    }

    /**
     * 新增、修改时更新干系人
     *
     * @param entity
     */
    private void insertStakeholder(BmsNotes entity) {
        //删除之前得关联表
        iBmsNotesStakeholderService.deleteBmsNotesStakeholderByGuid(entity.getGuid());
        String[] stakeholders = entity.getStakeholderId().split(",");
        for (String stakeholder : stakeholders) {
            BmsNotesStakeholder bmsNotesStakeholder = new BmsNotesStakeholder();
            bmsNotesStakeholder.setNotesId(entity.getGuid());
            bmsNotesStakeholder.setStakeholderId(stakeholder);
            iBmsNotesStakeholderService.insertBmsNotesStakeholder(bmsNotesStakeholder);
        }

    }

    /**
     * 新增、修改时更新文件表
     *
     * @param entity
     */
    private void updateAttachment(BmsNotes entity) {
        String[] attachmentGuids = entity.getAttachmentGuids();
        for (String attachmentGuid : attachmentGuids) {
            BmsAttachment bmsAttachment = new BmsAttachment();
            bmsAttachment.setGuid(attachmentGuid);
            bmsAttachment.setBizId(entity.getGuid());
            bmsAttachment.setCategory(AttachmentConstants.NOTES);
            bmsAttachment.setDelFlag("0");
            iBmsAttachmentService.updateBmsAttachment(bmsAttachment);
        }

    }

    /**
     * 批量删除便签
     *
     * @param guids 需要删除的便签ID
     * @return 结果
     */
    @Override
    public int deleteBmsNotesByGuids(String[] guids) {
        int ret = bmsNotesMapper.deleteBmsNotesByGuids(guids);
        if (ret > 0) {
            //同时删除标签与干系人的关联表
            iBmsNotesStakeholderService.deleteBmsNotesStakeholderByGuids(guids);
        }
        return ret;
    }

    /**
     * 删除便签信息
     *
     * @param guid 便签ID
     * @return 结果
     */
    @Override
    public int deleteBmsNotesByGuid(String guid) {
        return bmsNotesMapper.deleteBmsNotesByGuid(guid);
    }
}
